package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import org.joda.time.DateTime;

import database.DatabaseExtension;
import database.MyCountry;
import database.MyDestination;
import database.MySupplier;
import database.MyTransport;
import database.NewTirDataBase;

import Other.Glob;

import enums.DialogType;
import enums.SearchType;

public class Transport extends JFrame {

	//variable for singleton
	private static Transport transportInstance = null;
	//variable that represent this window frame
	private JFrame thisFrame = this;
	//variable to enable and disable the screen
	boolean disable;
	private JPanel contentPane;
	private JTextField tfCode;
	private JTextField tfLorry;
	private JTextField tfDeparture;
	private JTextField tfAgent;
	private JTextField tfDestinationCode;
	private JTextField tfDestinationName;
	private JTextField tfSupplierName;
	private JTextField tfSupplierCode;
	private JTextField tfLineCode;
	private JTextField tfLineName;
	private JTextArea tfArea;


	/**
	 * Create the frame.
	 */
	private Transport() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 856, 383);
		setTitle("Transport");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JButton btnRemove = new JButton("F6 Remove");
		//set up F6 for removing
		btnRemove.getInputMap(btnRemove.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
		btnRemove.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 //remove item when F6 button pressed
				ScreenHandling.remove(tfCode, SearchType.TRANSPORT);
			    }
		});
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//remove item when button pressed
				ScreenHandling.remove(tfCode, SearchType.TRANSPORT);
			}
		});
		
		JButton btnExit = new JButton("Esc Exit");
		
		//set up Escape for exit
		btnExit.getInputMap(btnExit.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
		btnExit.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				if (disable == false){
				//escape from the screen
					escape();
				}
			    }
		});
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//if the screen is disable it is possible to escape
				if (disable == false){
					escape();
				}
			}
		});
		
		
		
		JButton btnReset = new JButton("F7 Reset");
		
		//set up F7 for reset
		btnReset.getInputMap(btnReset.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "");
		btnReset.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				//reset field when button pressed
				 resetFields();
			    }
		});
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//reset field when button pressed
				resetFields();
			}
		});
		
		JButton btnSave = new JButton("F4 Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Save when button pressed
				Glob.setType(SearchType.TRANSPORT);
				try {
					saveDialog();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGap(0, 839, Short.MAX_VALUE)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(18)
					.addComponent(btnRemove)
					.addPreferredGap(ComponentPlacement.RELATED, 436, Short.MAX_VALUE)
					.addComponent(btnExit)
					.addGap(29)
					.addComponent(btnReset)
					.addGap(34)
					.addComponent(btnSave)
					.addGap(18))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGap(0, 84, Short.MAX_VALUE)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSave)
						.addComponent(btnRemove)
						.addComponent(btnReset)
						.addComponent(btnExit))
					.addContainerGap(38, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new LineBorder(new Color(0, 0, 0)));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 410, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(panel_2, 0, 0, Short.MAX_VALUE)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE))
		);
		
		JLabel lblComments = new JLabel("Comments");
		
		tfArea = new JTextArea();
		
		JScrollPane scrollPane = new JScrollPane(tfArea);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGap(21)
					.addComponent(lblComments)
					.addGap(31)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblComments)
					.addContainerGap(47, Short.MAX_VALUE))
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
		);
		panel_3.setLayout(gl_panel_3);
		
		JLabel lblDestination = new JLabel("Destination");
		
		tfDestinationCode = new JTextField();
		tfDestinationCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					ScreenHandling.enterData(tfDestinationCode, tfDestinationName, tfSupplierCode, SearchType.DESTINATION);
					System.out.println("button is being pressed");
					
				}
			}
		});
		//set up F5 for searching only when the text is focused
		tfDestinationCode.getInputMap(tfDestinationCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfDestinationCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.TRANSPORTDESTINATION;
				 
				search();
				
			    }
		});
		
		
		tfDestinationCode.setColumns(10);
		
		tfDestinationName = new JTextField();
		tfDestinationName.setEditable(false);
		tfDestinationName.setColumns(10);
		
		JButton sbDestination = new JButton("F5 Search");
		sbDestination.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.TRANSPORTDESTINATION;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		
		
		JButton sbSupplier = new JButton("F5 Search");
		sbSupplier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.TRANSPORTSUPPLIER;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		
		tfSupplierName = new JTextField();
		tfSupplierName.setEditable(false);
		tfSupplierName.setColumns(10);
		
		tfSupplierCode = new JTextField();
		tfSupplierCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					
					ScreenHandling.enterData(tfSupplierCode, tfSupplierName, tfLineCode, SearchType.SUPPLIER);
					System.out.println("button is being pressed");
					
				}
			}
		});
		//set up F5 for searching only when the text is focused
		tfSupplierCode.getInputMap(tfSupplierCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfSupplierCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.TRANSPORTSUPPLIER;
				 
				search();
				
			    }
		});
		tfSupplierCode.setColumns(10);
		
		JLabel lblSupplier = new JLabel("Supplier");
		
		JLabel lblNewLabel = new JLabel("Line");
		
		tfLineCode = new JTextField();
		tfLineCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					
					ScreenHandling.enterDataArea(tfLineCode, tfLineName, tfArea, SearchType.COUNTRY);
					System.out.println("button is being pressed");
					
				}
			}
		});
		//set up F5 for searching only when the text is focused
		tfLineCode.getInputMap(tfLineCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfLineCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.TRANSPORTCOUNTRY;
				 
				search();
				
			    }
		});
		tfLineCode.setColumns(10);
		
		tfLineName = new JTextField();
		tfLineName.setEditable(false);
		tfLineName.setColumns(10);
		
		JButton sbLine = new JButton("F5 Search");
		sbLine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.TRANSPORTCOUNTRY;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(19)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(lblDestination, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblSupplier, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
						.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(tfLineCode, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(tfLineName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(sbLine, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_2.createSequentialGroup()
									.addComponent(tfDestinationCode, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(tfDestinationName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_2.createSequentialGroup()
									.addComponent(tfSupplierCode, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(tfSupplierName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)))
							.addGap(18)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING)
								.addComponent(sbDestination, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
								.addComponent(sbSupplier, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))))
					.addGap(25))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGap(25)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblDestination)
								.addComponent(tfDestinationCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfDestinationName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGap(21)
							.addComponent(sbDestination)))
					.addGap(18)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGap(3)
							.addComponent(lblSupplier))
						.addComponent(tfSupplierCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(tfSupplierName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(sbSupplier))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(tfLineName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(sbLine)
						.addComponent(tfLineCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addContainerGap(57, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JLabel label = new JLabel("Code");
		
		tfCode = new JTextField();
		tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "Save");
		tfCode.getActionMap().put("Save", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				//Save when button pressed
				 Glob.type = SearchType.TRANSPORT;
				 try {
						saveDialog();
					} catch (Exception b) {
						// TODO Auto-generated catch block
						b.printStackTrace();
					}
			 }
		});
		tfCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a number is entered with ENTER
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					
					enterData();
					
					
				}
				
			}
		});
		//set up F5 for searching only when the text is focused
		tfCode.getInputMap(tfCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.TRANSPORT;
				 
				search();
				
			    }
		});
		tfCode.setColumns(10);
		
		JButton sbCode = new JButton("F5 Search");
		sbCode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 Glob.type = SearchType.TRANSPORT;
				 //when textcode focused and f5 pressed search dialog comes up
				search();
			}
		});
		//set up F5 for searching only when the text is focused
		tfCode.getInputMap(tfCode.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
		tfCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 //when textcode focused and f5 pressed search dialog comes up
				 Glob.type= SearchType.TRANSPORT;
				 
				search();
				
			    }
		});
		
		JLabel lblLorry = new JLabel("Lorry");
		
		tfLorry = new JTextField();
		tfLorry.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfDeparture.requestFocus();				
				}
			}
		});
		
	
				tfLorry.getInputMap(tfLorry.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "");
				tfLorry.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						tfCode.requestFocus();
					    }
				});
		tfLorry.setColumns(10);
		
		JLabel lblDeparture = new JLabel("Departure");
		
		tfDeparture = new JTextField();
		tfDeparture.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfAgent.requestFocus();				
				}
			}
		});
		
		tfDeparture.getInputMap(tfDeparture.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "");
		tfDeparture.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
			
				 String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
				
				 tfDeparture.setText(timeStamp);
				
			    }
		});
		tfDeparture.setColumns(10);
		
		JLabel lblAgent = new JLabel("Agent");
		
		tfAgent = new JTextField();
		tfAgent.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfDestinationCode.requestFocus();				
				}
			}
		});
		tfAgent.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(24)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblAgent, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
							.addGap(34)
							.addComponent(tfAgent))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(lblDeparture, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblLorry, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(label, GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(10)
									.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
									.addGap(10)
									.addComponent(sbCode, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
								.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
									.addGap(10)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(tfDeparture, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfLorry, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE))))))
					.addContainerGap(63, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(20)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(4)
							.addComponent(label))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(1)
							.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(sbCode))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblLorry))
						.addComponent(tfLorry, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(21)
							.addComponent(lblDeparture))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(18)
							.addComponent(tfDeparture, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(21)
							.addComponent(lblAgent))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(18)
							.addComponent(tfAgent, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
	
	//singleton
	public static Transport getInstance(){
		if(transportInstance == null){
			transportInstance = new Transport();
		}
		return transportInstance;
		}
	
	public void init(MyTransport transport){
		
		
		
		tfCode.setText(transport.getTransportID());	

		tfLorry.setText(transport.getLorry());
		
		
		if(String.valueOf(transport.getDeparture()).equals("null")){
			
			tfDeparture.setText("");
			
		}else{
			tfDeparture.setText(transport.getDeparture());
			
		}
		
		tfAgent.setText(transport.getAgent());
	
if(String.valueOf(transport.getDestinationCode()).equals("null")){
			
tfDestinationCode.setText("");
			
		}else{
			tfDestinationCode.setText(transport.getDestinationCode());
		}
		tfDestinationName.setText(transport.getDestinationName());
		
		if(String.valueOf(transport.getSupplierCode()).equals("null")){
			
			tfSupplierCode.setText("");
						
					}else{
						tfSupplierCode.setText(transport.getSupplierCode());
					}
					tfSupplierName.setText(transport.getSupplierName());
				
					if(String.valueOf(transport.getCountryCode()).equals("null")){
						
						tfLineCode.setText("");
									
								}else{
									tfLineCode.setText(transport.getCountryCode());
								}
								tfLineName.setText(transport.getCountryName());
								
								tfArea.append(transport.getComments());

	}
	
	public void initDestination(MyDestination destination){
		
		tfDestinationCode.setText(destination.getDestinationID());
		tfDestinationName.setText(destination.getName());
		
	}
	
public void initSupplier(MySupplier supplier){
		
		tfSupplierCode.setText(supplier.getSupplierID());
		tfSupplierName.setText(supplier.getName());
		
	}
	
public void initCountry(MyCountry country){
	
	tfLineCode.setText(country.getCountryID());
	tfLineName.setText(country.getCountryName());
	
}

//method needed to set focus from other screens
		public void setTextCodeFocus(){
			tfCode.requestFocus();
		}
		
		//reset fields
		public void resetFields(){
		tfCode.setText("");
		tfLorry.setText("");
		tfDeparture.setText("");
		tfAgent.setText("");
		tfDestinationCode.setText("");
		tfDestinationName.setText("");
		tfSupplierCode.setText("");
		tfSupplierName.setText("");
		tfLineCode.setText("");
		tfLineName.setText("");
		tfArea.setText("");
		tfCode.requestFocus();

		}

		public String getFieldCode() {
		    return tfCode.getText();
		 }
		
		
		//method for saving the object
		public void saveDialog() throws Exception{
			Glob.setDialog(DialogType.OVERWRITE);
			try{
				if(tfCode.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null, "Transport code may not be empty");
					tfCode.requestFocus();
					return;
				}
				if(tfDestinationCode.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null, "Destination code may not be empty");
					tfDestinationCode.requestFocus();
					return;
				}
			
				if(tfDeparture.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null, "Departure may not be empty");
					tfDeparture.requestFocus();
					return;
				}
			
				if(tfLorry.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null, "Lorry may not be empty");
					tfLorry.requestFocus();
					return;
				}
				
			
			String departure = tfDeparture.getText();
	
			
		
			
		
				
					if(!departure.isEmpty()){
					try {
						String[] s1 = departure.split("-");
						DateTime dt = new DateTime(Integer.parseInt(s1[0]),Integer.parseInt(s1[1]),Integer.valueOf(s1[2]),0,0);
					}catch(Exception e) {
						// Error message
						JOptionPane.showMessageDialog(null, "Departure must be a proper date");
						tfDeparture.requestFocus();
						return;
					
					}
			}
					
	
		
			
					
					ScreenHandling.checkIdOnTable(tfDestinationCode, SearchType.DESTINATION);		
			

			
		
			ScreenHandling.checkIdOnTable(tfSupplierCode, SearchType.SUPPLIER );
			

			
			ScreenHandling.checkIdOnTable(tfLineCode, SearchType.COUNTRY);
			
			
				
			ArrayList<String> ids = DatabaseExtension.columnIDsString("Select * from transport");
			//System.out.println(ids);
			boolean Ok = false;
			if(tfCode.getText().isEmpty()) {
				//Do nothing
				}else{
					
			for (String i: ids) {
				if (i.equalsIgnoreCase(tfCode.getText()))
						Ok=true;
			}		
				if (Ok) {
				//JOptionPane.showMessageDialog(null, "This ID is not on the Tax table");
				Dialog d = Dialog.getInstance();
				d.setVisible(true);
				Transport.getInstance().disableTransport();
				//tfCode.requestFocus();
				return;
				
			}
				}
				if(Ok==false){
				save();			
				}
				
			}catch (NumberFormatException e){
				
				System.out.println("This is the general expetion inside saveDialog");
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCode.setText("");
				tfCode.requestFocus();
				e.printStackTrace();
				return;
			 
			}
				
		}
		
		//method that saves the object in the database
		public void save(){
			  Glob.setType(SearchType.TRANSPORT);
		 
		
			
			String[] data = new String[8];	
			
			try{
			data[0] = tfCode.getText();	
			}catch(Exception code){
				
				code.printStackTrace();
				
			}
			System.out.println("This is the id got from the tfcode inside save" + data[0]);
			if(data[0].isEmpty()){
				JOptionPane.showMessageDialog(null, "Transport code may not be empty");
				tfCode.requestFocus();
				return;
			}
			
			data[1] = tfLorry.getText();
			
			data[2] = tfDeparture.getText();
			

			
			data[3] = tfAgent.getText();
			
			try{
				if(tfDestinationCode.getText().isEmpty()){
					
					data[4] = null;
					
				}else{
					
					data[4] = tfDestinationCode.getText();
				}

			}catch(Exception destination){
				destination.printStackTrace();
				JOptionPane.showMessageDialog(null, "Destination must be a proper integer number");
				return;
			}
		
			try{
				if(tfSupplierCode.getText().isEmpty()){
					
					data[5] = null;
					
				}else{
					//System.out.println("The ELSE has happeneded");
					data[5] = tfSupplierCode.getText();
				}

			}catch(Exception supplier){
				supplier.printStackTrace();
				JOptionPane.showMessageDialog(null, "Supplier must be a proper integer number");
				return;
			}
	
		
		try{
			if(tfLineCode.getText().isEmpty()){
				
				data[6] = null;
				
			}else{
				//System.out.println("The ELSE has happeneded");
				data[6] = tfLineCode.getText();
			}

		}catch(Exception line){
			line.printStackTrace();
			JOptionPane.showMessageDialog(null, "Line must be a proper integer number");
			return;
		}
		

		
		data[7] = tfArea.getText();
		
		
			MyTransport transport = new MyTransport(data);
			//System.out.println("this is the data inside de object in the method save" + Tax.getTaxID() + Tax.getTaxName());
			try{
			DatabaseExtension.saveTransport(transport);
			}catch (Exception saveShipment){
				
				saveShipment.printStackTrace();
				System.out.println("This is the exception released in saving shipment");
				JOptionPane.showMessageDialog(null, "Enter a correct format on the date fields 'yyyy-mm-dd', please");
				tfDeparture.requestFocus();
				return;
				
				
			}
				resetFields();
				setTextCodeFocus();
				
				
				

		}
		
		public void search(){
			
			SearchWindow s = SearchWindow.getInstance();
			s.setTitle();
			s.setVisible(true);
			disableTransport();
		}
		
		
		//method to disable the screen
		public void disableTransport(){
			Transport.getInstance().disable();
			disable = true;
		}
		
		//method to enable the screen
		public void enableTransport(){
			Transport.getInstance().enable();
			disable = false;
			if (Glob.type ==  SearchType.TRANSPORT){
				tfCode.requestFocus();
			}else if (Glob.type ==  SearchType.TRANSPORTDESTINATION){
				tfDestinationCode.requestFocus();
			}else if (Glob.type ==  SearchType.TRANSPORTSUPPLIER){
				tfSupplierCode.requestFocus();
			}else if (Glob.type ==  SearchType.TRANSPORTCOUNTRY){
				tfLineCode.requestFocus();
			}
			
			Transport.getInstance().toFront();
		}
		//method to get the disable boolean
		public boolean getDisable(){
			return disable;
		}
	
		//method that populates the screen fields with the object
		public void enterData(){
			try{
				//set up global variable for the Shipment screen
				Glob.setType(SearchType.TRANSPORT);
				//if textfield is 0 or less than 0
				
				if(tfCode.getText().isEmpty()){
					
					//there may be an indication here
					
				}
				
			
			NewTirDataBase c = (MyTransport) DatabaseExtension.getTableID("SELECT * FROM transport WHERE transportID = '" + tfCode.getText() +"'")[0];
			
			tfLorry.setText(((MyTransport) c).getLorry());
			
			try{
				//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
				if(String.valueOf(((MyTransport) c).getDeparture()).equals("null")){
					
					tfDeparture.setText("");
					
				}else{
					tfDeparture.setText(((MyTransport) c).getDeparture());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the departure");
					e.printStackTrace();
				}
			
			tfAgent.setText(((MyTransport) c).getAgent());
			
			try{
				//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
				if(String.valueOf(((MyTransport) c).getDestinationCode()).equals("null")){
					
					tfDestinationCode.setText("");
					
				}else{
					tfDestinationCode.setText(((MyTransport) c).getDestinationCode());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the destination code");
					e.printStackTrace();
				}
			
			tfDestinationName.setText(((MyTransport) c).getDestinationName());
			
			try{
				//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
				if(String.valueOf(((MyTransport) c).getSupplierCode()).equals("null")){
					
					tfSupplierCode.setText("");
					
				}else{
					tfSupplierCode.setText(((MyTransport) c).getSupplierCode());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the supplier code");
					e.printStackTrace();
				}
			
			tfSupplierName.setText(((MyTransport) c).getSupplierName());
			
			try{
				//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
				if(String.valueOf(((MyTransport) c).getCountryCode()).equals("null")){
					
					tfLineCode.setText("");
					
				}else{
					tfLineCode.setText(((MyTransport) c).getCountryCode());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the line code");
					e.printStackTrace();
				}
			
			tfLineName.setText(((MyTransport) c).getCountryName());
			
			

			
			
			tfArea.setText(((MyTransport) c).getComments());
			
			tfLorry.requestFocus();
			
			System.out.println("intro has been pressed");
			//requestfocus for the next field when pressing enter
			
			
			}catch(ArrayIndexOutOfBoundsException exception){
				System.out.println("This is the exception release when id not in database");
				
				exception.printStackTrace();
				
				
				tfLorry.requestFocus();
			}catch(Exception exception){
				System.out.println("General Exception");
				exception.printStackTrace();
			}
		}
		
		public void escape(){
			Transport.getInstance().dispose();
			Menu.getInstance().enableMenu();
		}
		
		public void codeFocus(){
			
			tfCode.requestFocus();
		}
		
}
